import ky from 'ky';

import { Post } from '../types';

type PostSummary = {
  postId?: number;
  title: string;
  content: string;
  format: 'md' | 'html';
};

export const getSubTreeItems = (postId: number) => ky.get('/api/post/all', { searchParams: { postId } }).json();

export const getPost = (postId: number): Promise<Post> => ky.get('/api/post/' + postId).json();

export const getPostList = (): Promise<Post[]> => ky.get('/api/post').json();

export const savePost = async ({ postId, title, content, format }: PostSummary): Promise<{ postId: number }> => {
  return await ky.post('/api/post/', { json: { postId, title, content, format } }).json();
};
